# import pymongo.cursor
# from datetime import datetime
from bson.objectid import ObjectId
from db.basic_db import connection
class DBBase:
    def __init__(self):
        db = connection.mydb
        self.weibocol = db.weibocol # 微博
        self.weixincol = db.weixincol # 微信
        self.wbusercountcol = db.wbusercountcol
        self.wxlogincodecol = db.wxlogincodecol
    '''
    添加微博搜索信息
    '''
    def insert_weibo(self, data):

        result_int = self.weibocol.insert(data)

        return result_int
    # '''
    # 添加微信搜索信息
    # '''
    # def insert_weixin(self, title, content, contentcreated, created_content_username, keyword, admin_id,):
    #     try :
    #         searchtime = str(datetime.now())
    #         data = {'title': title,
    #                 'content': content,
    #                 'created_content_username': created_content_username,
    #                 'contentcreated': contentcreated,
    #                 'keyworld': keyword,  # 关键字
    #                 'admin_id': admin_id,  # 输入这个关键字的人
    #                 'searchtime': searchtime,  # 执行搜索的时间
    #                 }
    #         result_int = self.weixincol.insert(data)
    #         one = self.weixincol.find_one({'title': title})
    #         print(one)
    #     except:
    #         pass
    #     return result_int
    '''
    添加微信搜索信息
    '''
    def insert_weixin(self, data):

        self.weixincol.insert(data)
    '''
    根据微信ID搜索微信
    '''
    def find_weixin_byid(self, id):

        return self.weixincol.find_one({'weixin_id': str(id)})

    '''
        根据微博ID搜索
    '''
    def find_weibo_byid(self, id):

        return self.weibocol.find_one({'weibo_id': str(id)})
    '''
    添加微博用户账号和密码以及cookies
    '''
    def insert_wb_usercount(self, adminid, wb_us, wb_pw, wb_cookies):

        data = {
            'adminid': adminid,
            'wb_us': wb_us,
            'wb_pw': wb_pw,
            'wb_cookies': wb_cookies,
        }
        return self.wbusercountcol.insert(data)
    '''
    根据ID查询微博用户登录的cookies
    '''
    def find_wbcookies_byid(self, id):
        return self.wbusercountcol.find_one({'_id': ObjectId(id)})
    '''
    存储微信登陆二维码
    '''
    def insert_wx_qcode(self, data):

        return self.wxlogincodecol.insert(data)

    '''
    根据adminID和rquestUUID查询用户登录二维码
    '''
    def find_wxqcode_byid(self,adminid, requstuuid):

        return self.wxlogincodecol.find_one({'adminid': adminid,'requstuuid': requstuuid})
    '''
    根据条件添加微信用户登录cookie
    '''
    def insert_wx_cookies(self,adminid,requstuuid,cookies):

        return self.wxlogincodecol.update({"adminid":adminid,"requstuuid":requstuuid},
                                   {"$set":{"cookies":cookies}})
    '''
    根据条件查询微信用户登录cookies
    '''
    def find_wxcookies_by(self,adminid,requstuuid,):
        return self.wxlogincodecol.find_one({'adminid':adminid,'requstuuid':requstuuid})

    '''
    分页查询微信采集信息
    '''
    def find_wx(self,page, weixin_keyword=None):
        page = int(page)
        if page is not 0:
            page = (page - 1)*10
        if weixin_keyword is not None:
            return self.weixincol.find({"weixin_keyword":weixin_keyword},{"_id":0},).skip(page).limit(10)
        else:
            return self.weixincol.find({},{"_id": 0,"weixin_cont":0}).skip(page).limit(10)
    '''
    分页查询微信采集信息
    '''
    def find_wb(self,page, weixin_keyword=None):
        page = int(page)
        if page is not 0:
            page = (page - 1)*10
        if weixin_keyword is not None:
            return self.weibocol.find({"weibo_keyword":weixin_keyword},{"_id":0},).skip(page).limit(10)
        else:
            return self.weibocol.find({}, {"_id": 0},).skip(page).limit(10)
    def test(self):
        self.weixincol.insert({"test":"test"})
if __name__=="__main__":
    dbbase = DBBase()
    dbbase.test()
    # keyword = None
    # data = dbbase.find_wx(1,keyword):
    # result_json = ''
    # total = data.count(with_limit_and_skip = False )
    # pagecount = int((total + 10 - 1) / 10)
    # print(pagecount)
    # for i in data:
    #     result_json = result_json + str(i) + ','
    # data = '[' + result_json + ']'
    # print("{'data':" + data + ",'total':" + str(total) +",'pagecount':"+ str(pagecount) + "}")




